import 'package:dio/dio.dart';
import 'http_response.dart';
import 'http_transformer.dart';

class AppHttpTransformer extends HttpTransformer {
  Function? fromJson;

  AppHttpTransformer({Function? fromJson}){
    this.fromJson=fromJson;
  }

  @override
  HttpResponse parse(Response response) {
    if (response.data["code"] == 0 ) {
      if(fromJson != null && response.data["data"] != null){
        return HttpResponse.success(fromJson!(response.data["data"]));
      }else{
        return HttpResponse.success(response.data["data"]);
      }
    } else {
      return HttpResponse.failure(
          errorMsg: response.data["message"], errorCode: response.data["code"]);
    }
  }
}
